package com.tuocent.dagv2.controller;

import com.alibaba.fastjson.JSONObject;
import com.tuocent.dagv2.common.AjaxResult;
import com.tuocent.dagv2.entity.Task;
import com.tuocent.dagv2.service.IProjectsService;
import com.tuocent.dagv2.service.ITaskService;
import com.tuocent.dagv2.service.ITaskWorkProcedureService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Controller
@RequestMapping("/project")
public class ProjectsController {

    @Autowired
    private IProjectsService projectsService;
    @Autowired
    private ITaskService taskService;
    @Autowired
    private ITaskWorkProcedureService workProcedureService;

    @RequestMapping("projectInfo")
    public String projectInfo(Model model) {
        return projectsService.projectInfo(model);
    }

    @GetMapping("getProjectInfo")
    @ResponseBody
    public AjaxResult getProjectInfo(@RequestParam("page") Integer page,
                                     @RequestParam("limit") Integer limit) {
        return projectsService.getProjectInfo(page, limit);
    }

    @PostMapping("importProject")
    @ResponseBody
    public AjaxResult importProject(@RequestParam("file") MultipartFile file) throws Exception {
        return projectsService.importProject(file);
    }

    /**
     * 删除项目
     *
     * @param data
     * @return
     */
    @PostMapping("delProject")
    @ResponseBody
    public AjaxResult delProject(@RequestBody Map data) {
        return projectsService.delProject(data);
    }

    @GetMapping("setProjectStart")
    public String setProjectStart(Model model,
                                  @RequestParam("id") String id) {
        return projectsService.setProjectStart(model, id);
    }

    @GetMapping("setProjectFinish")
    public String setProjectFinish(Model model,
                                   @RequestParam("id") String id) {
        return projectsService.setProjectFinish(model, id);
    }

    @PostMapping("projectStart")
    @ResponseBody
    public AjaxResult projectStart(@RequestBody Map<String, Object> data) {
        return projectsService.projectStart(data);
    }

    @PostMapping("projectFinish")
    @ResponseBody
    public AjaxResult projectFinish(@RequestBody Map<String, Object> data) {
        return projectsService.projectFinish(data);
    }

    @GetMapping("taskInfo")
    public String taskInfo(Model model) {
        return taskService.taskInfo(model);
    }

    @GetMapping("getTaskInfo")
    @ResponseBody
    public AjaxResult getTaskInfo(@RequestParam("pid") String pid,
                                  @RequestParam(value = "page", defaultValue = "1") Integer page,
                                  @RequestParam(value = "limit", defaultValue = "9999999") Integer limit) {
        return taskService.getTaskInfo(pid, page, limit);
    }


    @GetMapping("setTaskInfo")
    public String setTaskInfo(Model model,
                              @RequestParam("id") String id,
                              @RequestParam("pid") String pid) {
        return taskService.setTaskInfo(model, id, pid);
    }

    @RequestMapping("saveTaskInfo")
    @ResponseBody
    public AjaxResult saveTaskInfo(HttpSession session,
                                   @RequestBody Task data) {
        return taskService.saveTaskInfo(session, data);
    }

    @PostMapping("delTaskInfo")
    @ResponseBody
    public AjaxResult delTaskInfo(@RequestParam("id") String id) {
        return taskService.delTaskInfo(id);
    }

    @GetMapping("setTaskStart")
    public String setTaskStart(Model model,
                               @RequestParam("id") String id) {
        return taskService.setTaskStart(model, id);
    }

    @PostMapping("taskStart")
    @ResponseBody
    public AjaxResult taskStart(@RequestBody Map data) {
        return taskService.taskStart(data);
    }

    @GetMapping("setTaskFinish")
    public String setTaskFinish(Model model,
                                @RequestParam("id") String id) {
        return taskService.setTaskFinish(model, id);
    }


    @PostMapping("taskFinish")
    @ResponseBody
    public AjaxResult taskFinish(@RequestBody Map data) {
        return taskService.taskFinish(data);
    }

    /**
     * 设置用户任务
     *
     * @param model
     * @param userId
     * @return
     */
    @GetMapping("setUserTask")
    public String setUserTask(Model model,
                              @RequestParam("userId") String userId) {
        return taskService.setUserTask(model, userId);
    }

    @GetMapping("getUserTask")
    @ResponseBody
    public AjaxResult getUserTask(@RequestParam("pid") String pid,
                                  @RequestParam("userId") String userId) {
        return taskService.getUserTask(pid, userId);
    }

    @GetMapping("getUserProjectTask")
    @ResponseBody
    public AjaxResult getUserProjectTask(@RequestParam("pid") String pid,
                                         @RequestParam("userId") String userId) {
        return taskService.getUserProjectTask(pid, userId);
    }

    @GetMapping("getUserTaskInfo")
    @ResponseBody
    public AjaxResult getUserTaskInfo(HttpSession session,
                                      @RequestParam("pid") String pid,
                                      @RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "limit", defaultValue = "9999999") Integer limit) {
        return taskService.getUserTaskInfo(session, pid, page, limit);
    }

    @PostMapping("saveUserTask")
    @ResponseBody
    public AjaxResult saveUserTask(@RequestBody JSONObject data) {
        return taskService.saveUserTask(data);
    }


    /**
     * 设置任务工序
     *
     * @param model
     * @param id
     * @return
     */
    @GetMapping("setTaskWorkProcedure")
    public String setTaskWorkProcedure(Model model,
                                       @RequestParam("id") String id,
                                       @RequestParam("pid") String pid) {
        return workProcedureService.setTaskWorkProcedure(model, id, pid);
    }

    @GetMapping("getTaskWorkProcedure")
    @ResponseBody
    public AjaxResult getTaskWorkProcedure(@RequestParam("tid") String tid,
                                           @RequestParam("pid") String pid) {
        return workProcedureService.getTaskWorkProcedure(tid, pid);
    }

    @PostMapping("saveTaskWorkProcedure")
    @ResponseBody
    public AjaxResult saveTaskWorkProcedure(@RequestBody JSONObject data) {
        return workProcedureService.saveTaskWorkProcedure(data);
    }

    /**
     * 设置用户任务工序
     *
     * @param model
     * @param userId
     * @return
     */
    @GetMapping("setUserWorkProcedure")
    public String setUserWorkProcedure(Model model,
                                       @RequestParam("userId") String userId) {
        return workProcedureService.setUserWorkProcedure(model, userId);
    }


    @GetMapping("getUserWorkProcedure")
    @ResponseBody
    public AjaxResult getUserWorkProcedure(@RequestParam("tid") String tid,
                                           @RequestParam("pid") String pid,
                                           @RequestParam("userId") String userId) {
        return workProcedureService.getUserWorkProcedure(tid, pid, userId);
    }

    @PostMapping("saveUserWorkProcedure")
    @ResponseBody
    public AjaxResult saveUserWorkProcedure(@RequestBody JSONObject data) {
        return workProcedureService.saveUserWorkProcedure(data);
    }
}
